<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\select2\Select2;
use app\modules\Cruise\models\Cruise;
use app\models\Supplier;
use yii\helpers\ArrayHelper;

$all = Cruise::find()->all(); 
$all_cruise = (new \yii\db\Query())
    ->select(['COUNT(product.cruise_id) as cruise_num','product.cruise_id'])
    ->from('product')
    ->join('LEFT JOIN', 'cruise', 'cruise.id = product.cruise_id')
    ->where(['product.status'=>$model->status])
    ->andFilterWhere(['product.type'=>$model->type])
    ->andFilterWhere(['product.line'=>$model->line])
    ->andFilterWhere(['cruise.supplier_id'=>$model->supplier_id])
    ->andFilterWhere(['>=', 'product.go_time', $model->begin_go_time])
    ->andFilterWhere(['<=', 'product.go_time', $model->end_go_time])
    ->addGroupBy('product.cruise_id')
    ->orderBy('cruise_num desc')
    ->all();

$arr_cruise = yii\helpers\ArrayHelper::map($all, 'id', 'name');
$cruise = [];
//邮轮
if(!empty($all_cruise)){
    foreach($all_cruise as $v){
        $cruise[$v['cruise_id']] = sprintf("%s(%s)",$arr_cruise[$v['cruise_id']],$v['cruise_num']);
    }
}

   
$all_line = (new \yii\db\Query())
    ->select(['COUNT(product.line) as line_num','product.id','product.line'])
    ->from('product')
    ->join('LEFT JOIN', 'cruise', 'cruise.id = product.cruise_id')
    ->where(['product.status'=>$model->status])
    ->andFilterWhere(['product.type'=>$model->type])
    ->andFilterWhere(['product.cruise_id'=>$model->cruise_id])
    ->andFilterWhere(['cruise.supplier_id'=>$model->supplier_id])
    ->andFilterWhere(['>=', 'product.go_time', $model->begin_go_time])
    ->andFilterWhere(['<=', 'product.go_time', $model->end_go_time])
    ->addGroupBy('product.line')
    ->orderBy('line_num desc')
    ->all();
//邮轮航线
$lines = ArrayHelper::map($all_line,'line',function ($element) {
    return sprintf("%s(%s)",yii::$app->params['line'][$element['line']],$element['line_num']);
});    

$supplier_all = Supplier::find()->where(['status'=>1])->all(); 
$all_supplier = (new \yii\db\Query())
    ->select(['COUNT(cruise.supplier_id) as supplier_num','cruise.supplier_id'])
    ->from('product')
    ->join('LEFT JOIN', 'cruise', 'cruise.id = product.cruise_id')
    ->where(['product.status'=>$model->status])
    ->andFilterWhere(['product.type'=>$model->type])
    ->andFilterWhere(['product.cruise_id'=>$model->cruise_id])
    ->andFilterWhere(['product.line'=>$model->line])
    ->andFilterWhere(['>=', 'product.go_time', $model->begin_go_time])
    ->andFilterWhere(['<=', 'product.go_time', $model->end_go_time])
    ->addGroupBy('cruise.supplier_id')
    ->orderBy('supplier_num desc')
    ->all();

$arr_supplier = yii\helpers\ArrayHelper::map($supplier_all, 'id', 'company');
$supplier = [];
//邮轮
if(!empty($all_supplier)){
    foreach($all_supplier as $v){
        $supplier[$v['supplier_id']] = sprintf("%s(%s)",$arr_supplier[$v['supplier_id']],$v['supplier_num']);
    }
}

   
$all_type = (new \yii\db\Query())
    ->select(['COUNT(product.type) as type_num','product.id','product.type'])
    ->from('product')
    ->join('LEFT JOIN', 'cruise', 'cruise.id = product.cruise_id')
    ->where(['product.status'=>$model->status])
    ->andFilterWhere(['product.line'=>$model->line])
    ->andFilterWhere(['product.cruise_id'=>$model->cruise_id])
    ->andFilterWhere(['cruise.supplier_id'=>$model->supplier_id])
    ->andFilterWhere(['>=', 'product.go_time', $model->begin_go_time])
    ->andFilterWhere(['<=', 'product.go_time', $model->end_go_time])
    ->addGroupBy('product.type')
    ->orderBy('type_num desc')
    ->all();
//邮轮航线类型
$types = ArrayHelper::map($all_type,'type',function ($element) {
    return sprintf("%s(%s)",yii::$app->params['product_type'][$element['type']],$element['type_num']);
}); 

?>
<style type="text/css">
    .form-group{margin-bottom: 20px !important;}
</style>

<div class="product-search">

    <?php $form = ActiveForm::begin([
        'id' => 'line-form',
        'action' => ['lines'],
        'method' => 'get',
        'layout' => 'inline',
        //'options' => ['data-pjax'=>true,],
    ]); ?>

      <?= $form->field($model, 'begin_go_time')->widget(kartik\date\DatePicker::classname(),['options' => ['placeholder' => '选择出发开始时间'],
                        'pluginOptions' => ['format' => 'yyyy-mm-dd','autoclose'=>true,'todayHighlight' => true]
      ])->label("开始时间:");  ?>
     
      <?= $form->field($model, 'end_go_time')->widget(kartik\date\DatePicker::classname(),['options' => ['placeholder' => '选择出发结束时间'],
                        'pluginOptions' => ['format' => 'yyyy-mm-dd','autoclose'=>true,'todayHighlight' => true]
      ])->label("结束时间:");  ?>
    
  
        <?php  echo $form->field($model, 'supplier_id')->dropdownList($supplier, ['prompt'=>'选择邮轮公司'])->label("选择邮轮公司：") ?>
      
         <?php  echo $form->field($model, 'cruise_id')->dropdownList($cruise, ['prompt'=>'选择邮轮'])->label("选择邮轮：") ?>
     
  
        <?php  echo $form->field($model, 'type')->dropdownList($types, ['prompt'=>'选择航线类型'])->label("选择航线类型：") ?>
      
        <?php  echo $form->field($model, 'line')->dropdownList($lines, ['prompt'=>'选择邮轮航线'])->label("选择邮轮航线：") ?>
    
        <?php  echo $form->field($model, 'status')->dropdownList(Yii::$app->params['status'], ['prompt'=>Yii::t('app','Status')]) ?>

      <div class="form-group">
         <?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
      </div>
    

    <?php ActiveForm::end(); ?>

</div>
